package package1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

public class TestForJsonParse {

	public static String testFileAddr = "C:\\Users\\User\\Desktop\\json";

	public static void main(String[] args) throws IOException {
		makeTestJsonFile();
		String jsonStr = read(testFileAddr);
		JSONObject jsonObj = getJsonObj(jsonStr);
		
		Integer allCount = (Integer) jsonObj.get("allCount");
		System.err.println(allCount);
		Integer sq = (Integer) jsonObj.get("qStatus");
		System.err.println(sq);

	}

	private static void makeTestJsonFile() throws IOException {
		String content = "{\"allCount\":4,\"qStatus\":1,\"reason\":\"查询成功\",\"status\":1,\"quesList\":[{\"content\":\"20160420002\",\"imageUrl\":\"http://sse.roadshow.com/sseroadshow/resources/upload/images/201604/1460943100026.jpg\",\"uname\":\"乐视网123\",\"crtTime\":\"2小时前\",\"type_id\":5},{\"qtitle\":\"副总经理,董事\",\"content\":\"阿斯顿撒旦\",\"answerContent\":\"20160420  1\",\"answerName\":\"刘弘\",\"qname\":\"刘弘\",\"answerImage\":\"http://sse.roadshow.com/sseroadshow/resources/upload/images/201604/1460964886354.jpg\",\"imageUrl\":\"http://sse.roadshow.com/sseroadshow/resources/images/1015.png\",\"uname\":\"乐视网\",\"answerTime\":\"2小时前\",\"crtTime\":\"04月18日 09:47\",\"type_id\":1},{\"qtitle\":\"总经理,董事长\",\"content\":\"3232232321\",\"answerContent\":\"同志，你这是提的什么问题啊ad.fsd\",\"answerName\":\"贾跃亭\",\"qname\":\"贾跃亭\",\"answerImage\":\"http://sse.roadshow.com/sseroadshow/resources/images/person_man.jpg\",\"imageUrl\":\"http://sse.roadshow.com/sseroadshow/resources/images/1015.png\",\"uname\":\"乐视网\",\"answerTime\":\"04月18日 09:45\",\"crtTime\":\"04月18日 09:44\",\"type_id\":1},{\"content\":\"路演正式开始了。！！！！\",\"imageUrl\":\"http://sse.roadshow.com/sseroadshow/resources/upload/images/201604/1460943100026.jpg\",\"uname\":\"刘弘(副总经理,董事)\",\"crtTime\":\"04月18日 09:42\",\"type_id\":5}]}";
		// content = "aaa";
		FileWriter fw = new FileWriter(testFileAddr);
		fw.write(content);
		fw.flush();
	}

	private static void print(List<Map> videoMapList) {
		for (Map m : videoMapList)
			print(m);
	}

	private static void print(Map m) {
		if (m.size() == 0) {
			System.err.println("m size=0");
			return;
		}
		for (Object key : m.keySet()) {
			System.err.println(key + ":" + m.get(key));
		}
	}

	private static String read(String f) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(new File(f)));
		String temp;
		StringBuffer sb = new StringBuffer();
		while ((temp = br.readLine()) != null) {
			sb.append(temp);
		}
		return sb.toString();
	}

	private static net.sf.json.JSONObject getJsonObj(String str) throws IOException {
		return net.sf.json.JSONObject.fromObject(str);
	}

	private static String getDesktop() {
		return System.getenv("USERPROFILE") + "\\desktop";
	}
}
